<template>
  <div>
    <el-row>
      <el-col :span="3"> 
        <el-input v-model="job_type" placeholder="请输入工种名称"></el-input>
      </el-col>
      <el-col :span="4">
        <el-button @click="job_type_click" class="add_btn"  icon="el-icon-search"> 搜索</el-button>
        <el-button @click="add_jobtype" icon="el-icon-plus" class="add_btn">新增工种</el-button>
      </el-col>
    </el-row>

    <el-table :data="dataList" stripe style="width: 100%" border>
      <el-table-column prop="id" label="ID">
      </el-table-column>
      <el-table-column prop="job_type" label="工种名称">
      </el-table-column>
      <el-table-column prop="multi_order_num" label="可领订单数">
      </el-table-column>
      <el-table-column prop="print_num" label="工单打印数">
      </el-table-column>
    <!--  <el-table-column prop="sort" label="流程顺序">
      </el-table-column> -->
      <el-table-column prop="is_part" label="是否参与">
        <template slot-scope="scope">
          <span>{{scope.row.is_part==1?'是':'否'}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="is_multi" label="是否多个订单">
        <template slot-scope="scope">
          <span>{{scope.row.is_multi==1?'是':'否'}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="is_restrict" label="是否限制流程">
        <template slot-scope="scope">
          <span>{{scope.row.is_restrict==1?'是':'否'}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="updated_at" label="创建时间">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button @click="set_product(scope.row)" type="text" icon="el-icon-edit">编辑</el-button>
          <el-popconfirm confirm-button-text='确定' cancel-button-text='取消' icon="el-icon-info" icon-color="red"
            title="是否删除这条数据？" @confirm='del_product(scope.row)'>
            <el-button slot="reference" type="text" icon="el-icon-delete"> 删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" background @prev-click='prev'
        @next-click='next' :current-page.sync="pagination.page" :page-size="pagination.per_page"
        layout="total, prev, pager, next" :total="total">
      </el-pagination>
    </div>


    <el-dialog :title="title" :visible.sync="isProduct" width="30%" :close-on-click-modal='false'>
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="工种名称" prop="job_type">
          <el-input v-model="ruleForm.job_type"></el-input>
        </el-form-item>
        <!-- <el-form-item label="流程顺序" prop="sort">
          <el-input v-model="ruleForm.sort" type="number"></el-input>
        </el-form-item> -->
        <el-form-item label="是否参与" prop="is_part">
          <el-radio-group v-model="ruleForm.is_part">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="2">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否多个订单" prop="is_multi">
          <el-radio-group v-model="ruleForm.is_multi">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="2">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="可领订单数" prop="multi_order_num">
          <el-input v-model="ruleForm.multi_order_num" type="number"></el-input>
        </el-form-item>
        <el-form-item label="工单打印数">
          <el-input v-model="ruleForm.print_num" type="number"></el-input>
        </el-form-item>
        <el-form-item label="是否限制流程" prop="is_restrict">
          <el-radio-group v-model="ruleForm.is_restrict">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="2">否</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isProduct = false">取 消</el-button>
        <el-button type="primary" @click="submitadd('ruleForm')">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {
    getJobTypeList,
    createJobType,
    delJobType
  } from '@/api/product/index'
  export default {
    data() {
      return {
        job_type:'',
        title: '',
        isProduct: false,
        dataList: [],
        pagination: {
          per_page:15,
          page: 1,
        },
        total: 0,
        ruleForm: {
          id: '',
          job_type: '',
          is_part:1,
          // sort: '',
          is_multi:1,
          multi_order_num: '',
          is_restrict:1,
          print_num:1
        },
        rules: {
          job_type: [{required: true,message: '请输入工种',trigger: 'blur'}],
          multi_order_num:[{required: true,message: '请输入可领订单数',trigger: 'blur'}],
        }
      }
    },
    mounted() {
      this.getlist()
    },
    methods: {
      set_product(row){
        this.ruleForm=row
        this.title = '编辑工种'
        this.isProduct = true
      },
      add_jobtype() {
        this.title = '新增工种'
        this.isProduct = true
        this.$nextTick(function(){
          this.$refs.ruleForm.resetFields()
        })
      },
      submitadd(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            createJobType(this.ruleForm).then(res => {
              if (res.code == 200) {
                this.isProduct = false
                this.$message.success('提交成功')
                this.getlist()
              }
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      getlist() {
        getJobTypeList({...this.pagination,...this.ser}).then(res => {
          if (res.code == 200) {
            this.dataList = res.data.data
            this.total = res.data.total
          }
        })
      },
      handleSizeChange(val) {
        
        console.log(`每页 ${val} 条`);
      },
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`);
        this.pagination.page = val
        this.getlist()
      },
      prev() {
        this.pagination.page = this.pagination.page - 1
        this.getlist()
      },
      next() {
        
        this.pagination.page = this.pagination.page + 1
        this.getlist()
      },
      del_product(row) {
        delJobType({
          id: row.id,
          is_del: 1
        }).then(res => {
          if (res.code == 200) {
            this.isProduct = false
            this.$message.success('提交成功')
            this.getlist()
          }
        })
      },
      job_type_click(){
        this.pagination= {
            per_page: 10,
            page: 1,
        }
        getJobTypeList({job_type:this.job_type,...this.pagination}).then(res => {
          if (res.code == 200) {
            this.dataList = res.data.data
            this.total = res.data.total
          }
        })
      }
    }
  }
</script>

<style>
</style>
